<?php
defined('BASEPATH') OR exit('No direct script access allowed');


use QCloud_WeApp_SDK\Mysql\Mysql as DB;


class GetCoupon extends CI_Controller
{
    public function index()
    {
        $pid = $_GET['pid'];
        $userid = $_GET['userid'];


        $rows = null;
        if ($pid && $userid) {
            $rows = DB::row('cc_coupon',['*'], ['userid' => $userid, 'pid' => $pid]);
            if(!$rows){
                $rows = DB::insert('cc_coupon', ['userid' => $userid, 'pid' => $pid,'has_coupon' => 1]);
            }else{
                $rows = DB::update('cc_coupon', ['userid' => $userid, 'pid' => $pid,'has_coupon' => 1]);
            }
        }
        if ($rows > 0) {
            $this->json([
                'code' => 0,
                'data' => $rows
            ]);
        } else {
            $this->json([
                'code' => -1,
                'error' => $rows['error']
            ]);
        }
    }
}
